package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {


    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckItemMapper checkItemMapper;

    /**
     * 添加检查组，同时需要关联检查项
     * @param checkitemIds
     * @param checkGroup
     */
    @Transactional
    public void add(String checkitemIds, CheckGroup checkGroup) {
        //1.添加检查组，操作t_checkgroup表
        checkGroupMapper.add(checkGroup);
        //2.操作中间表，需要向t_checkgroup_checkitem表添加数据
        //需要获取到检查组id
        if(checkitemIds != null && checkitemIds.length() > 0){
            String[] ids = checkitemIds.split(",");
            for (String id : ids) {
                log.info("检查组id：{}",checkGroup.getId());
                // 判断检查项id在t_checkitem表中是否存在
                CheckItem checkItem = checkItemMapper.getBookingById(Integer.parseInt(id));
                if(checkItem == null){
                    return;
                }
                log.info("检查项id：{}",id);
                checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getId(),Integer.parseInt(id));
            }
        }

    }

    /**
     * 根据检查组id删除检查组
     * @param id
     */
    public void deleteById(Integer id) {
        checkGroupMapper.deleteById(id);
    }

    /**
     * 根据检查组id查询检查组关联的检查项id
     * @param id
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {

        List<Integer> checkGroups = checkGroupMapper.findCheckGroupIdsBySetmealId(id);
        return checkGroups;
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 编辑检查组，同时需要更新检查项的id
     * @param checkitemIds
     * @param checkGroup
     */
    public void edit(String checkitemIds, CheckGroup checkGroup) {
        if (checkGroupMapper.edit(checkGroup) == 0){
            return;
        }
        checkGroupMapper.deleteCheckGroupCheckItem(checkGroup.getId());
        if(checkitemIds != null && checkitemIds.length() > 0){
            String[] ids = checkitemIds.split(",");
            for (String id : ids) {
                log.info("检查组id：{}",checkGroup.getId());
                // 判断检查项id在t_checkitem表中是否存在
                CheckItem checkItem = checkItemMapper.getBookingById(Integer.parseInt(id));
                if(checkItem == null){
                    return;
                }
                log.info("检查项id：{}",id);
                checkGroupMapper.addCheckGroupAndCheckItem(checkGroup.getId(),Integer.parseInt(id));
            }
        }
    }

    /**
     * 查询所有检查组
     * @return
     */
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    /**
     * 根据检查组id查询检查组信息
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }
}
